﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DbHelp;

namespace IotherRis
{
    public partial class Fr_PrintTemplate : G3Control.G3FormA
    {

        public Fr_PrintTemplate()
        {
            InitializeComponent();

        }
        uDataSetC mDsPrintTemplateOne = null;
        string mSate = "";

        private void grid_p_template_DoubleClick(object sender, EventArgs e)
        {
            int mHand = gridView1.FocusedRowHandle;
            if (mHand < 0)
            {
                MessageBox.Show("请选择需要维护的数据");
            }
            string strPatid = gridView1.GetRowCellValue(mHand, "IID").ToString();

            string mSql = "select * from pi_printtemplate where IID=" + strPatid + "";
            mDsPrintTemplateOne = new uDataSetC(mSql, AccessHelper.mReportDb);

            if (mDsPrintTemplateOne.RowCount() > 0)
            {
                txt_name.Text = mDsPrintTemplateOne.GetItemString(1, "TEMPLATE_NAME");
                txt_sql.Text = AccessHelper.f_ByteToStr(mDsPrintTemplateOne.GetItemByte(1, "TEMPLATE_SQL"));
                txt_modality.Text = mDsPrintTemplateOne.GetItemString(1, "MODALITY");
                txt_data.Text = AccessHelper.f_ByteToStr(mDsPrintTemplateOne.GetItemByte(1, "TEMPLATE_CONTNET"));
                mSate = "维护中";
            }

        }

        private void Fr_PrintTemplate_Load(object sender, EventArgs e)
        {
            f_loadPrintTemplateData();
        }

        public void f_loadPrintTemplateData()
        {
            string mSql = "";
            mSql = "select * from pi_printtemplate  where STATE<>'已删除'";
            uDataSetC mDsPrintTemplate = new uDataSetC(mSql, AccessHelper.mReportDb);
            grid_p_template.DataSource = mDsPrintTemplate.Tables[0];
        }

        private void btn_add_Click(object sender, EventArgs e)
        {

            if (mDsPrintTemplateOne == null)
            {

                string mSql = "select * from pi_printtemplate where IID=-1";
                mDsPrintTemplateOne = new uDataSetC(mSql, AccessHelper.mReportDb);
                mDsPrintTemplateOne.InsertRow();
            }

            if (mSate == "维护中")
            {
                if (MessageBox.Show("该模版尚未保存，是否继续新增", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    txt_name.Text = "";
                    txt_sql.Text = "";
                    txt_modality.Text = "";
                    txt_data.Text = "";
                    mSate = "新增中";

                }
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (mDsPrintTemplateOne == null)
                {
                    MessageBox.Show("未新增或修改模版,不能保存");
                    return;
                }
                mDsPrintTemplateOne.SetItem(1, "TEMPLATE_NAME", txt_name.Text);
                mDsPrintTemplateOne.SetItem(1, "TEMPLATE_SQL", AccessHelper.f_strToByte(txt_sql.Text));
                mDsPrintTemplateOne.SetItem(1, "MODALITY", txt_modality.Text);
                mDsPrintTemplateOne.SetItem(1, "TEMPLATE_CONTNET", AccessHelper.f_strToByte(txt_data.Text));
                mDsPrintTemplateOne.Update(AccessHelper.mReportDb);

                MessageBox.Show("保存成功");
                f_loadPrintTemplateData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败" + ex.Message);
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (mDsPrintTemplateOne == null)
            {

                MessageBox.Show("没有打开模版,不能删除");
            }
            else
            {
                if (MessageBox.Show("确认删除该模版", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    mDsPrintTemplateOne.SetItem(1,"STATE","已删除");
                    mDsPrintTemplateOne.Update();
                    MessageBox.Show("删除成功");
                    f_loadPrintTemplateData();
                }
            }
        }
    }
}
